#ifndef CUENTACORRIENTEMODULE_H
#define CUENTACORRIENTEMODULE_H

#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QDialog>
#include <QSqlDatabase>
#include <QSqlRelationalTableModel>
#include <QTableView>
#include <QMessageBox>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QLineEdit>
#include <QDateEdit>
#include <QLabel>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlQuery>
#include <QSqlError>
#include <QModelIndex>
#include <QSplitter>
#include <QRadioButton>
#include <QFileDialog>
#include <QDir>
#include <QPrinter>
#include <QTextDocument>
#include <QTextCursor>
#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>

#include "CustomToolbar.h"
#include "AltaCuentaCorriente.h"
#include "ModificacionCuentaCorriente.h"
#include "ABMPersona.h"
#include "ABMConcepto.h"
#include "GenerarRecibo.h"
#include "EditClient.h"

class CuentaCorrienteModule : public QMainWindow
{
    Q_OBJECT
public:
    explicit CuentaCorrienteModule(QWidget *parent = 0);

signals:

public slots:
    void AltaCCBtn();
    void RemoveCCBtn();
    void EditCCBtn();
    void UpdateListsBtn();
    void ABMConceptoBtn();
    void ImprimirReciboBtn();
    void BuscarRazonSocial();
    void AbrirInfoCliente();

    void SeleccionarCC();
    void FiltrarCobroServicio();
    void FiltrarDeudasServicio();
    void FiltrarCobroEsporadico();
    void FiltrarDeudasEsporadico();
    void EncontrarPersona();
    void FiltroPersona();
    void EncontrarAnio();
    void FiltroAnio();

private:
    CustomModulesToolbar *cc_toolbar;
    QAction *action_alta_cc;
    QAction *action_remove_cc;
    QAction *action_edit_cc;
    QAction *action_update_cc;
    QAction *action_abmPersona;
    QAction *action_abmConcepto;
    QAction *action_recibo_cc;

//    QSqlTableModel *client_cc_model;
//    QTableView *client_cc_view;

    QRadioButton *filter_servicio;
    QRadioButton *filter_servicio_deuda;
    QRadioButton *filter_esporadico;
    QRadioButton *filter_esporadico_deuda;
    QRadioButton *filter_cliente;
    QRadioButton *filter_anio;

    QString filtroCC;
    QString cliente;
    QString tipoCobro;
    QString anio;

    bool servicioChecked;
    bool servicioDeudaChecked;
    bool esporadicoChecked;
    bool esporadicoDeudaChecked;

    QSqlRelationalTableModel *cobro_mensual_model;

    void UpdateCCView();
    void RefreshCCView();
    void UpdateClientList();
    void UpdateCCList();
    void RefreshClientCCView();
    void CleanCCList();

    void ActualizarDeuda(QString client_selected);
    void ControlVencimientos();

    void AltaCCToolbar();
    void RemoveCCToolbar();
    void ModificacionCCToolbar();
    void ActualizarListasToolbar();
    void ABMPersonaToolBar();
    void ABMConceptoToolBar();
    void ImprimirReciboToolbar();

    void GenerarReciboClientePDF(QString path, QString raz_soc);
};

class CtaCteSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    CtaCteSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        if(role == Qt::BackgroundRole)
        {
            // Fondo: Si excedente pago, darkBlue.
            QModelIndex index3 = index.model()->index(index.row(), 4, index.parent()); // get index of "watched" column
            if (index.column() != 4) {
                if (index3.data(Qt::DisplayRole).toDouble() < 0)
                    return QVariant(QColor(Qt::darkBlue));
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toDouble() < 0)
                    return QVariant(QColor(Qt::darkBlue));
            }

            // Fondo: Si debe, rojo. Si pago, verde. Si bonificado darkCyan.
            QModelIndex index2 = index.model()->index(index.row(), 6, index.parent()); // get index of "watched" column
            if (index.column() != 6) {
                if (index2.data(Qt::DisplayRole).toString() == "Pagó")
                    return QVariant(QColor(Qt::darkGreen));
                else {
                    if (index2.data(Qt::DisplayRole).toString() == "Debe")
                        return QVariant(QColor(Qt::red));
                    else {
                        if (index2.data(Qt::DisplayRole).toString() == "Bonificado")
                            return QVariant(QColor(Qt::darkCyan));
                    }
                }
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "Pagó")
                    return QVariant(QColor(Qt::darkGreen));
                else {
                    if (value.toString() == "Debe")
                        return QVariant(QColor(Qt::red));
                    else {
                        if (value.toString() == "Bonificado")
                            return QVariant(QColor(Qt::darkCyan));
                    }
                }
            }
        }

        if(role == Qt::ForegroundRole)
        {
            // Letra: Si debe o pago, blanca.
            QModelIndex index4 = index.model()->index(index.row(), 6, index.parent()); // get index of "watched" column
            if (index.column() != 6) {
                if (index4.data(Qt::DisplayRole).toString() == "Pagó")
                    return QVariant(QColor(Qt::white));
                else {
                    if (index4.data(Qt::DisplayRole).toString() == "Debe")
                        return QVariant(QColor(Qt::white));
                    else {
                        if (index4.data(Qt::DisplayRole).toString() == "Bonificado")
                            return QVariant(QColor(Qt::white));
                    }
                }
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "Pagó")
                    return QVariant(QColor(Qt::white));
                else {
                    if (value.toString() == "Debe")
                        return QVariant(QColor(Qt::white));
                    else {
                        if (value.toString() == "Bonificado")
                            return QVariant(QColor(Qt::white));
                    }
                }
            }
        }

        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 0 || col == 2 || col == 5 || col == 7 || col == 8 || col == 9) {
                return QVariant(Qt::AlignVCenter | Qt::AlignHCenter);
            }
            if (col == 3) {
                return QVariant(Qt::AlignVCenter | Qt::AlignLeft);
            }
            if (col == 4 || col == 11) {
                return QVariant(Qt::AlignVCenter | Qt::AlignRight);
            }
        }

        if (role == Qt::FontRole) {
            int col = index.column();
            if (col == 4 || col == 11)
                return QVariant (QFont("Helvetica57-Condensed",13,13));
            else
                return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

class ClienteCteCtaSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    ClienteCteCtaSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        QSqlRelationalTableModel *cc_model = new QSqlRelationalTableModel();
        QString filtroCC = "estado_deuda = 'Debe'";

        if(role == Qt::BackgroundColorRole)
        {
            if (index.column() == 1) {
                QModelIndex index2 = index.model()->index(index.row(), 1, index.parent()); // get index of "watched" column
                QString razonSocial = index2.data(Qt::DisplayRole).toString();
                QString Filtro = "cc_razon_social = '" + razonSocial + "' and " + filtroCC;
                cc_model->setTable("cuenta_corriente");
                cc_model->setFilter(Filtro);

                QSqlDatabase().transaction();
                if (!cc_model->select()) {
                    QSqlDatabase().rollback();
                }
                else {
                    QSqlDatabase().commit();
                    QString rSocial;
                    int rowCount = cc_model->rowCount();
                    for (int i = 0; i < rowCount; ++i) {
                        rSocial = cc_model->index(i, 1, index.parent()).data(Qt::DisplayRole).toString();
                        if (index2.data(Qt::DisplayRole).toString() == rSocial) {
                            return QVariant(QColor(Qt::red));
                        }
                    }
                }
            }
        }

        if(role == Qt::ForegroundRole)
        {
            if (index.column() == 1) {
                QModelIndex index2 = index.model()->index(index.row(), 1, index.parent()); // get index of "watched" column
                QString razonSocial = index2.data(Qt::DisplayRole).toString();
                QString Filtro = "cc_razon_social = '" + razonSocial + "' and " + filtroCC;
                cc_model->setTable("cuenta_corriente");
                cc_model->setFilter(Filtro);

                QSqlDatabase().transaction();
                if (!cc_model->select()) {
                    QSqlDatabase().rollback();
                }
                else {
                    QSqlDatabase().commit();
                    QString rSocial;
                    int rowCount = cc_model->rowCount();
                    for (int i = 0; i < rowCount; ++i) {
                        rSocial = cc_model->index(i, 1, index.parent()).data(Qt::DisplayRole).toString();
                        if (index2.data(Qt::DisplayRole).toString() == rSocial) {
                            return QVariant(QColor(Qt::white));
                        }
                    }
                }
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // CUENTACORRIENTEMODULE_H
